<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 设备管理控制器
 */
class EquipmenteController extends AdminController{

/**
 * 设备列表
 */
public function index(){
  
    $arg['page']=I('get.page',1,'strip_tags');
    $item=D('Device');
    $devices=$item->index($arg);
   

       /*设备类型*/
          $type=M('config')->where('id=%d',2)->getField('value');
          $type=unserialize($type);
          $this->assign('type',$type);
          $this->assign('devices',$devices['devices']);
          $this->assign('pagearr',$devices['pagearr']);
           $this->display();

}

/**
 * 添加设备
 */
public function append(){
if(IS_POST){
  $data['tiypeid']=$_POST['tiypeid'];
    $data['devicecode']=$_POST['devicecode'];
    $where['shopid']=$_POST['cateid'];
    $de['deviceid']=$_POST['devicecode'];
    $re=M('device')->add($data);
$res=M('shop_user')->where($where)->save($de);
    if($re>0){
        $this->redirect("Equipmente/index");
    }

}else{
  $where['deviceid']='';
$companyname=M('shop_user')->where($where)->Field("shopid,companyname")->select();

$this->assign('companyname',$companyname);

  $this->display();

}


}



/**
 * 检测设备唯一
 */
    public function weiyi(){
      $where['devicecode']=$_GET['re'];
    $re=M('device')->where($where)->select();
    if(empty($re)){
      $data = array('status'=>200,'code'=>'success','info'=>'无效的手机号');
          echo json_encode($data);
    }else{
      $data = array('status'=>404,'code'=>'error','info'=>'该标识已存在');
          echo json_encode($data);
    }

    }



            /**
             * 冻结设备
             */
        public function frozen(){
            $where['shopid']=$_GET['id'];
            $data['isdel']=1;
            M('shop_user')->where($where)->save($data);
            $this->display("Equipmente/index");
        }


        /**
         * 设备编辑
         */
         public function edit(){
            if(IS_POST)
            {
              $yid=$_SESSION['yid'];
            $data['tiypeid']=I('post.tiypeid','','strip_tags');
            $data['devicecode']=I('post.devicecode','','strip_tags');
            $data['shopid']=I('post.shopid',0,'strip_tags');
            $where['deviceid']=I('post.deviceid',0,'strip_tags');
            $yid=I('post.yid',0,'strip_tags');
            $data['yid']=!empty($yid)?$yid:$_SESSION['yid'];

            $re=M('device')->where($where)->save($data);
         
                  
            if($res ===false){
                 echo "<script type='text/javascript'>alert('修改失败'); window.history.go(-1);</script>";
            }else{
               $this->redirect("Equipmente/index");
            }
            }else{
                    //设备类型
                   $type=M('config')->where('id=%d',2)->getField('value');
                   $type=unserialize($type);
                   //获取所有加盟商
                 $yunying=M('shop_user')->where("founder='%d'",$_SESSION['yid'])->getField('shopid,companyname');
                       ////获取该运营商下的下一级运营商
                 $carrie=M('yunying_user')->where("pid='%d'",$_SESSION['yid'])->getField("yid,companyname");
                 
               
                   $device=M('device a');
                   $where['a.deviceid']=I('get.id',0,'strip_tags');
                   $where['a.deviceid']=intval($where['a.deviceid']);

                   $result=$device->join('left join '.C(DB_PREFIX).'shop_user b'.' on a.shopid=b.shopid ')
                                  ->where($where)
                                  ->getField('a.deviceid,a.devicecode,a.tiypeid,a.shopid,b.companyname,a.yid');
                                 
                 $this->assign('yunying',$yunying);
                 $this->assign('carrie',$carrie);///获取该运营商下的运营商
                    $this->assign('type',$type);
                    $this->assign('info',$result[$where['a.deviceid']]);
                    $this->display();
                   
                }
        }



        public function getEquipment(){
         
         $arg['typeid']=I('post.type',0,'strip_tags');
        $arg['isfen']=I('post.isfen',0,'strip_tags');
        $arg['keywords']=I('post.keywords','','strip_tags');
        $arg['page']=I('post.page',1,'strip_tags');
       
        $device=D('Device');
        $result=$device->index($arg);
    
       if(!empty($result['devices'])){
            $result['status']=1;
            
            echo json_encode($result,JSON_UNESCAPED_UNICODE);
            return '';

          }

          $result['status']=0;
          $result['message']='暂无数据';
          echo  json_encode($result,JSON_UNESCAPED_UNICODE);


        }

}
